<?php

use Illuminate\Support\Str;

if(!function_exists('createSn')) {
    function createSn($len = 32)
    {
        if($len == 12) {
            // 生成唯一哈希并截取部分字符
            $hash = md5(uniqid(mt_rand(), true));
            $numericHash = preg_replace('/[^0-9]/', '', $hash);

            // 时间戳部分（年月日）
            $datePart = date('ymd');

            // 组合并截取为12位
            return substr($datePart . $numericHash, 0, 12);
        } else {
            $microtime = explode(' ', microtime());

            $timestamp = str_replace('.', '', $microtime[1] . $microtime[0]);

            $random = Str::upper(Str::random(18));

            return substr($timestamp, -14) . $random;
        }
    }
}
